package com.sawen.houserent.controller;

import com.alibaba.fastjson.JSONObject;
import com.sawen.houserent.bean.Homeinfo;
import com.sawen.houserent.bean.User;
import com.sawen.houserent.service.HomeinfoService;
import com.sawen.houserent.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**@Controller*/
@RestController
public class HomeinfoController {
    @Autowired
    private HomeinfoService homeinfoService;

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     查询全部
    */
    @RequestMapping(value = "/queryHome",method = RequestMethod.POST)
    /**@ResponseBody*/
    public  List<Homeinfo> getHomeinfo(){
        String key = "user";
        ValueOperations opts = redisTemplate.opsForValue();
        boolean hasKey = redisTemplate.hasKey(key);
        List<Homeinfo>  homeinfoList=null;
        if(hasKey){
            homeinfoList=homeinfoService.getHomeinfo();
            String key1= "homelist";
            String homelistStr = JSONObject.toJSONString(homeinfoList);
            opts.set(key1,homelistStr,100, TimeUnit.SECONDS);
            return homeinfoList;

        }
        return null;


    }
    /**
     删除
     */
    @RequestMapping(value = "/{id}/delHome",method = RequestMethod.GET)
    @ResponseBody
    public void deleteByPrimaryKey(@PathVariable Integer id){
        homeinfoService.deleteByPrimaryKey(id);
    }
    /**
     修改
     */
    @RequestMapping(value = "/{id}/updateHome",method = RequestMethod.PUT)
    @ResponseBody
    public void updateByPrimaryKey(Homeinfo homeinfo){
        homeinfoService.updateByPrimaryKey(homeinfo);
    }
    /**
     添加
     */
    @RequestMapping(value ="/addHome",method = RequestMethod.POST)
    /**@ResponseBody*/
    public void  insertSelective(Homeinfo homeinfo){
        homeinfoService.insertSelective(homeinfo);
    }

}
